<template>
  <div ref="imageWrapper">
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="ruleForm" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="loadData">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="账套/核算单元" prop="unitCode">
                <a-input placeholder="点击选择" v-model="queryParam.accName" @click.native="openSearchModel"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="期间" prop="unitCode">
                <a-input placeholder="点击选择" v-model="queryParam.time" @click.native="openSearchModel"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="科目" prop="unitCode">
                <a-input
                  placeholder="点击选择"
                  v-model="queryParam.subjectName"
                  @click.native="openSearchModel"
                ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <!-- <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button> -->
                <a-button type="danger" @click="handleToYD(false)" icon="arrow-right" style="margin-left: 8px">
                  转疑点
                </a-button>
                <a-button
                  :loading="loadingExp"
                  type="primary"
                  style="margin-left: 8px"
                  icon="download"
                  @click="handleExportXls()"
                  :disabled="!(leftDataSource.length > 0)"
                  >导出</a-button
                >
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 操作按钮区域 -->
      <!-- <div class="table-operator">
      <a-button type="primary" @click="openSearchModel()">查询</a-button>
    </div> -->

      <div class="ant-alert ant-alert-info" style="margin: 16px 0">
        <span>辅助明细</span>
      </div>
      <a-row :gutter="24">
        <a-col :md="6" :sm="8" style="height: 650px; overflow-x: hidden; overflow-y: auto">
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            class="pointer_table"
            :columns="columns2"
            :dataSource="leftDataSource"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange"
            :row-selection="rowSelectionOp"
          >
            <template v-slot:assistGroupName="text">
              <j-ellipsis :value="text" :length="13" />
            </template>
            <template v-slot:subjectGroupName="text">
              <j-ellipsis :value="text" :length="15" />
            </template>
          </a-table>
        </a-col>
        <a-col :md="24 - 6" :sm="8">
          <!-- table区域-begin -->
          <!-- :customRow="handleClickRow2" -->
          <div>
            <a-table
              ref="table"
              size="middle"
              bordered
              rowKey="id"
              class="pointer_table"
              :columns="columns"
              :dataSource="dataSource"
              :pagination="ipagination"
              :loading="loading"
              @change="handleTableChange"
              :scroll="{ x: 1800 }"
            >
              <template v-slot:assistName="text">
                <j-ellipsis :value="text" :length="6" />
              </template>
              <template v-slot:accName="text">
                <j-ellipsis :value="text" :length="6" />
              </template>
              <template v-slot:unitName="text">
                <j-ellipsis :value="text" :length="6" />
              </template>
              <template v-slot:summary="text">
                <j-ellipsis :value="text" :length="6" />
              </template>
              <span slot="action" slot-scope="text, record">
                <a @click="handleClickRow2(record)">关联</a>
                <a-divider type="vertical" />
                <a style="color: red" @click="handleToYD(record)">转疑点</a>
              </span>
            </a-table>
          </div>
          <!-- table区域-end -->
        </a-col>
      </a-row>

      <SearchModal ref="searchModal" @ok="onSearch" @clear="searchReset"></SearchModal>
      <YDModel ref="YDModel" :routerData="$route.path" />
      <CheckAndEditModel ref="modalForm" @ok="updateProList"></CheckAndEditModel>
      <CheckOrganModal ref="departModal" @ok="updateQueryParam"></CheckOrganModal>
    </a-card>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { YzzxListMixin } from '../mixins/YzzxListMixin'
import SearchModal from './SearchModal'
import columns from './indexColumns'
import columns2 from './workColumns'
import { httpAction, getAction } from '@/api/manage'
import CheckAndEditModel from './ModalView/CheckAndEditModel'
import CheckOrganModal from '../components/CheckOrganModal'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  name: 'fzmx',
  mixins: [base64toImg, JeecgListMixin, YzzxListMixin, jumpConfigMixin],
  components: {
    SearchModal,
    CheckAndEditModel,
    CheckOrganModal,
    YDModel,
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          this.$nextTick(() => {
            this.onSearch(to.query)
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        } else if (Object.keys(to.params).length) {
          if (to.params.from == 'fzye') this.FzyeSearch(to.params)
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      isBackButton: false,
      queryParam: {},
      superParam: {
        subjectCode: undefined,
        subjectName: '',
      },
      // 表头
      columns: columns(this),
      columns2: columns2(this),
      leftDataSource: [],
      subjectName: '',
      loadingExp: false,
      url: {
        leftList: '/sjyzzx/sjZxzyAccountSubject/querySubjectByAssist',
        list: '/sjyzzx/sjZxzyAccountVouche/queryVoucheAssistDetailByCondition',
        exportXlsUrl: '/sjyzzx/sjZxzyAccountVouche/exportFzmxXls',
      },
      scodes: '',
      disableMixinCreated: true,
    }
  },
  created() {},
  mounted() {
    // this.openSearchModel()
  },
  computed: {
    rowSelectionOp() {
      return {
        type: 'radio',
        // getCheckboxProps: (record) => {
        //   if (this.checkedKeys.includes(record.id)) {
        //     record.index = 0
        //     this.checkedRows = [{ ...record }]
        //     this.model.assistType = record.assistCode
        //   }
        //   return {
        //     props: {
        //       // disabled: record.name === 'Disabled User',
        //       // name: record.name,
        //       defaultChecked: this.checkedKeys.includes(record.id), // 该属性用于默认勾选处理
        //     },
        //   }
        // },
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = [...selectedRowKeys]
          this.superParam.subjectCode = selectedRows[0].subjectCode
          this.superParam.subjectName = selectedRows[0].subjectName
          this.queryParam.assistCode = selectedRows[0].assistCode
          this.queryParam.assistType = selectedRows[0].assistType
          this.loadData(1)
        },
      }
    },
  },
  methods: {
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.leftDataSource = []
      this.dataSource = []
      this.ipagination = this.$options.data().ipagination
    },
    onSearch(param) {
      this.columns = columns(this)
      this.columns[1].title = param.assistTypeText + '编号'
      this.columns[2].title = param.assistTypeText + '名称'
      this.queryParam = Object.assign({}, param)
      this.scodes = param.subjectCode
      if (this.queryParam.timeType == 1) {
        this.queryParam.startTime = this.queryParam.startTime.split('-').join('')
        this.queryParam.endTime = this.queryParam.endTime.split('-').join('')
      }
      this.queryParam.time = `${param.startTime} ~ ${param.endTime}`
      this.leftDataSource = []
      this.dataSource = []
      this.getLeftTable()
    },
    openSearchModel() {
      this.$refs.searchModal.edit()
      this.$refs.searchModal.isCheck = false
    },
    handleExportXls() {
      this.loadingExp = true
      getAction(this.url.exportXlsUrl, { ...this.queryParam, ...this.superParam }).then((res) => {
        this.$message.success('正在下载，请稍等！')
        let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
        // let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
        window.open(url, '_blank')
        this.loadingExp = false
      })
    },
    getLeftTable() {
      this.loading = true
      let param = Object.assign({}, this.queryParam, { subjectName: '' })
      getAction(this.url.leftList, param).then((res) => {
        if (res.success) {
          this.leftDataSource = res.result.records || res.result
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleClickRow1(record, index) {
      return {
        on: {
          click: () => {
            this.queryParam.assistCode = record.assistCode
            this.queryParam.assistType = record.assistType
            this.queryParam.subjectCode = record.subjectCode
            this.queryParam.subjectName = record.subjectName
            this.subjectName = record.subjectGroupName
            this.loadData(1)
          },
        },
      }
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.queryParam.subjectName = ''
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      // this.dataSource=[]
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleClickRow2(record, index) {
      this.handleCheck(record, '凭证')
      // return {
      //   on: {
      //     dblclick: () => {
      //       this.handleCheck(record, '凭证')
      //     },
      //   },
      // }
      // this.$router.push({
      //   name: 'sjxmzygl-yzzx-jtpzcx',
      //   params: {
      //     from: 'fzmxz',
      //     param: this.queryParam,
      //     record,
      //   },
      // })
    },
    updateProList() {
      this.loadData(1)
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    handleToYD(record) {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr = undefined
        if (record) {
          ydnr = `【科目名称：${this.subjectName}】【凭证字号：${record.docCode}】`
        }
        let time = `【${startTime}～${endTime}】【辅助明细表】`
        this.addYd(this.queryParam, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    FzyeSearch(params) {
      let { param, record } = params
      // this.columns[1].title = param.assistTypeText + '编号'
      // this.columns[2].title = param.assistTypeText + '名称'
      // this.queryParam = param
      // this.scodes = param.subjectCode
      this.onSearch(Object.assign({}, param))
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
</style>
<style lang="less" scoped>
.pointer_table {
  /deep/ .ant-table-row {
    cursor: pointer;
  }
}
</style>